/////////////////////
//  Program Name    - Complement Puller
//  Program Rundown - This program pulls complements and displays them to the user
//  Current Version - V0.1 Dev
//

#include <iostream>
#include <conio.h>
#include <fstream>
#include <time.h>
#include "cursorio.h"
#include "defines.h"
#include "web.h"
using namespace std;

struct ComplementStringList {
	char m_Complement[256]; //Complements can only be up to 32 charractors long
	ComplementStringList *m_Nxt;
};

bool Update( char [128] );
void ClearList( void );
bool GetListFromFile( char [256] );
void AddToList( char [256] );
void PrintDebugList( void );
void PrintNumList( int );
void PrintHeader( void );

ComplementStringList *sHead= NULL;
int ComplementCount = 0;

int main() {
	
	//////////////--Variables--//////////////////
	char op;
	bool main_loop = true;
	
	//////////////--Load list--//////////////////
	if( Update( URL_LOCATION ) ) {
		//centern("Failed to set list");
	}
	_getch();
	//////////////--Main Loop--//////////////////
	srand( (unsigned)time(0) );
	while( main_loop ) {
		clrscr();
		PrintHeader();
		cout << "Options:\n\t1 - Give a random complement\n\t2 - About\n\tX - Exit\n  >>";
		op = _getch();
		switch( op ) {
			case '1':
				clrscr();
				PrintHeader();
				cout << "\n\n";
				centern("Here is your random complement:");
				cout << endl;
				textcolor(12);
				if( ComplementCount != 0 )
					PrintNumList( rand()%ComplementCount ); 
				else {
					textcolor(1);
					centern("There are no complements in the list");
				}
				textcolor(15);
				_getch();
			break;
			case '2': //About page
				clrscr();
				PrintHeader();
				cout << "\tAuthor:  Zack Mattor\n\n";
				cout << "\tVersion: V0.1 Dev\n\n";
				cout << "\tWebsite: http://Arksaw.com\n\n";
				_getch();
			break;
			case 'x':
				main_loop = false;
			break;	
		}
	}
}

bool Update( char UrlLocation[128] ) {
	clrscr();
	PrintHeader();
	textcolor(3);
	centern("Location:");
	centern(UrlLocation);
	cout << "\n\n";
	textcolor(12);
	center ("Downloading Update");
	dot(4, 300, '.' );
	cout << endl;
	if(GetFile(UrlLocation, "list.complements")) {
		center ("Clearing Current List");
		ClearList();
		dot(4, 100, '.' );
		cout << endl;
		center ("Updating List");
		if( !GetListFromFile("list.complements") ) {
			cout << "Cant load downloaded file";
			return false;
		}
		dot(4, 100, '.' );
		cout << endl;
		center ("Finished!");
	}
	else {
		textcolor(1);
		centern ("Failed to download list loading preset (if exist)!!!");
		GetListFromFile("list.complements");
	}
	textcolor(15);
	return true;
}

void ClearList() {
	ComplementCount = 0;
	sHead = NULL;
}

bool GetListFromFile( char FileName[256] ) {
	char Buff[256];
	ifstream File (FileName);
	if( File.is_open() ) {
		while( !File.eof() ) {
			File.getline( Buff, 255 );
			if( strlen(Buff) > 3 )
				AddToList( Buff );
		}
		File.close();
		return true;
	}
	else {
		PrintHeader();
		return false;
	}
}

void AddToList( char Buffer[256] ) {
	if( sHead == NULL ) {
		sHead = new ComplementStringList;
		strcpy( sHead->m_Complement, Buffer );
		sHead->m_Nxt = NULL;
	}
	else {
		ComplementStringList *TmpMovable = sHead;
		ComplementStringList *Tmp = new ComplementStringList;
		strcpy( Tmp->m_Complement, Buffer );
		Tmp->m_Nxt = NULL;
		while( TmpMovable->m_Nxt != NULL )
			TmpMovable = TmpMovable->m_Nxt;
		//cout << "FOUND END\n";
		TmpMovable->m_Nxt = Tmp;
	}
	ComplementCount ++;
}

void PrintDebugList( void ) {
	ComplementStringList *TmpMovable = sHead;
	while( TmpMovable->m_Nxt != NULL ) {
		cout << " -" << TmpMovable->m_Complement << endl;
		TmpMovable = TmpMovable->m_Nxt;
	}
	cout << " -" << TmpMovable->m_Complement << endl;
	TmpMovable = TmpMovable->m_Nxt;
}

void PrintNumList( int Num ) {
	if( sHead != NULL ) {
		ComplementStringList *TmpMovable = sHead;
		if( Num < ComplementCount && Num >= 0 ) {
			for( int x = 0; x < Num; x++ ) {
				TmpMovable = TmpMovable->m_Nxt;
			}
			center( TmpMovable->m_Complement );
			cout << endl;
		}
		else {
			textcolor(1);	
			centern( "That is not a valid key" );
		}
	}
	else {
		textcolor(1);
		center("List isnt made!");
	}
}

void PrintHeader( void ) {
	char buff[32] = "| ";
	int tmp;
	char buf[6];
	textcolor(2);
	itoa( ComplementCount, buf, 10 );
	strcat(buff, buf);
	strcat(buff, " complements loaded |");
	centern("-------------------------------------------");
	centern("| Zack's random complement puller v.1 Dev |");
	centern("-------------------------------------------");
	centern(buff);
	tmp = strlen(buff);
	strcpy(buff, "--");
	for( int x = 0; x < tmp-2; x++) {
		strcat(buff, "-");
	}
	centern(buff);
	textcolor(15);
	cout << endl << endl << endl;
}
